package com.lethelh.jdbc.sample;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.lethelh.jdbc.app.entity.Goods;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class CommonsDButilsDemo {
    public static void main(String[] args) {
        Properties properties = new Properties();
        String propertyFile = CommonsDButilsDemo.class.getResource("/druid-config.properties").getPath();
        Connection conn = null;
        String sql = "insert into goods(name,price,desp) values(?,?,?);";
        try {
            propertyFile = new URLDecoder().decode(propertyFile, "UTF-8");
            properties.load(new FileInputStream(propertyFile));
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
            conn = dataSource.getConnection();
            QueryRunner qr = new QueryRunner();
            qr.update(conn, sql, new Object[] {"小米手机", 1999, "晓龙875"});
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (conn != null && !conn.isClosed()){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
